package com.kingschan.jeasytag.tag.power;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.jsp.JspException;
import org.json.JSONArray;
import org.json.JSONObject;
import com.kingschan.jeasytag.core.annotations.tag.*;
import com.kingschan.jeasytag.core.parse.TagParseFactory;
import com.kingschan.jeasytag.tag.EasyTag;
import com.kingschan.jeasytag.tag.RootTag;
import com.kingschan.jeasytag.tag.form.ComboBoxTag;
import com.kingschan.jeasytag.tag.grid.ColumnTag;
import com.kingschan.jeasytag.tag.grid.GridAutoAnalysis;
import com.kingschan.jeasytag.tag.grid.GridTag;
import com.kingschan.jeasytag.tag.window.DialogTag;

/**
 * 查询框
 * @author kingschan
 *date:2013-4-18
 */

@TagMetaData(CssClass = "", tag = "form"
	,parseClass=""	
)public class SearchBoxTag extends RootTag implements Cloneable{
	private static final long serialVersionUID = 1L;
	private DialogTag dialog;
	private GridTag grid;
	private ComboBoxTag combobox;

	@PropertyField
	private List<SearchBoxParamTag> param;

	@Override
	protected Object clone() throws CloneNotSupportedException {		
		return super.clone();
	}
	@Override
	public int doStartTag() throws JspException {
		param= new ArrayList<SearchBoxParamTag>();
		final String  id_str =this.getId()+"_searchbox";

		//执行初始化
			DialogTag d = new DialogTag();
			d.setId(id_str);
			d.setTitle("查询配置框");
			d.setStyle("width:630px;height:300px;");
			d.setResizable("false");
			d.setModal("true");
			d.setClosed("true");			
			this.dialog=d;

			GridTag g = new GridTag();
			g.setColumns(new ArrayList<ColumnTag>());
			g.setId(id_str+"_grid");
			g.setHeight("257");
			g.setWidth("613");
			g.setRownumbers("true");
			g.setSingleSelect("true");
			//g.setOnClickRow("searchbox_row_onclick('"+id_str+"_grid',rowIndex, rowData);");
			g.setOnSelect("searchbox_row_onclick('"+id_str+"_grid',rowIndex, rowData);");			
			g.setToolbar("#"+this.getId()+"_searchbox_toolbar");

			ColumnTag c = new ColumnTag();
			c.setField("filterfield");
			c.setTitle("字段");
			c.setWidth("120");

			ColumnTag c1 = new ColumnTag();
			c1.setField("relationship");
			c1.setTitle("关系");
			c1.setWidth("120");
			c1.setEditor("{type:'combobox',options:{required:true,valueField:'value',textField:'text',editable:false,data:relationship_data,panelHeight:50}}");

			ColumnTag c2 = new ColumnTag();
			c2.setField("Operator");
			c2.setTitle("运算符");
			c2.setWidth("120");
			c2.setEditor("{type:'combobox',options:{required:true,valueField:'value',textField:'text',editable:false,data:Operator_data,panelHeight:220}}");

			ColumnTag c3 = new ColumnTag();
			c3.setField("value");
			c3.setTitle("值");
			c3.setWidth("220");
			c3.setEditor("{type:'text',options:{required:true}}");

			//ColumnTag c4 = new ColumnTag();
			//c4.setField("info");
			//c4.setTitle("info");
			///c4.setWidth("220");
			//c4.setHidden("true");

			g.addColumn(c);
			g.addColumn(c1);
			g.addColumn(c2);
			g.addColumn(c3);
			//g.addColumn(c4);
			this.grid=g;

			ComboBoxTag com = new ComboBoxTag();
			com.setWidth("200");
			com.setEditable("false");
			com.setId(id_str+"_fieldlist");
			com.setTextField("title");
			com.setValueField("field");
			com.setData(this.getId()+"_searchbox_data");			
			com.setOnSelect("select_field_change("+id_str+",record);");
			this.combobox=com;



		return super.doStartTag();
	}
	@Override
	public int doEndTag() throws JspException {		

		try {
			if (this.getParent() instanceof GridTag) {
				GridTag grid = (GridTag) this.getParent();	
				autoModelForGrid(grid);
			}

		} catch (Exception e) {
			e.printStackTrace();
		}

		return super.doEndTag();
	}
	/**
	 * 当你父标签为grid时的支持信息
	 * @param grid
	 * @throws Exception 
	 */
	public void autoModelForGrid(GridTag grid) throws Exception{
		JSONArray jsons = new JSONArray();
		JSONObject json =null;
		//加入辅助属性记录grid的id
		this.dialog.setEasytagdata("{target:'"+grid.getId()+"'}");
		SearchBoxTag sbt = (SearchBoxTag) this.clone();
		grid.setSearch(sbt);		
		List<ColumnTag> lis = grid.getColumns();
		for (ColumnTag c : lis) {
			if (null!=c.getEditor()) {
				json= new JSONObject();
				json.put("field", c.getField());
				json.put("title", c.getTitle());
				json.put("editor",c.getEditor());
				jsons.put(json);
			}
		}
		if (null!=this.param) {
			for (int i = 0; i < param.size(); i++) {
				json = new JSONObject();
				json.put("field", param.get(i).getField());
				json.put("title", param.get(i).getTitle());
				json.put("editor", param.get(i).getEditor());				
				jsons.put(json);
			}
		}

		getSuport().appendDefindScript(" var "+this.getId()+"_searchbox_data="+jsons.toString()+";");
	}
	/**
	 * 生成查询框
	 * target_id  目标id
	 * @return
	 */
	public String parse(EasyTag root)throws Exception{	
		StringBuffer sb = new StringBuffer();
		sb.append(TagParseFactory.executeParse(this.dialog,pageContext));		
		sb.append(TagParseFactory.executeParse(this.grid,pageContext));
		sb.append(new GridAutoAnalysis(this.grid).builderColumns());
		sb.append("</table>");
		sb.append("<div id=\""+this.getId()+"_searchbox_toolbar"+"\" style=\"padding:3px\"> ");
		sb.append("字段列表:");		
		sb.append(TagParseFactory.executeParse(this.combobox,pageContext));
		sb.append("</select>");
		sb.append("<a id=\"btna\" href=\"javascript:add_searchbox_filter('"+this.getId()+"_searchbox_grid');\" class=\"easyui-linkbutton\" data-options=\"iconCls:'icon-add',plain:true\">添加</a>");
		sb.append(" <a id=\"btnb\" href=\"javascript:del_searchbox_filter('"+this.getId()+"_searchbox_grid');\" class=\"easyui-linkbutton\" data-options=\"iconCls:'icon-cancel',plain:true\">删除</a>");
		sb.append("<a id=\"btnc\" href=\"javascript:query_for_grid('"+this.getId()+"_searchbox');\" class=\"easyui-linkbutton\" data-options=\"iconCls:'icon-search',plain:true\">查询</a>");
		sb.append("</div>");		
		sb.append("</div>");	     
	    return sb.toString();



	}

	/**
	 * 添加查询参数
	 * @param json
	 */
	public void addParam(SearchBoxParamTag sbt){	
		param.add(sbt);
	}
}
